<?php
/**
* @package plugin itemtype for Pages-and-Items
* @version 1.0.4
* @copyright Copyright (C) 2006-2008 Carsten Engel. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* @author http://www.pages-and-items.com
* @joomla Joomla is Free Software
*/

//no direct access
if(!defined('_VALID_MOS') && !defined('_JEXEC')){
	die('Restricted access');
}

class class_itemtype_youtube extends class_pi{
	
	function installer(){
		//plugins don't have their own install.php to install tables etc. when installed. don't ask me why. Here is the workaround:
				
		//get database
		if( defined('_JEXEC') ){
			//joomla 1.5
			$database = JFactory::getDBO();
			$pluginstable = 'plugins';		
		}else{
			//joomla 1.0.x
			global $database;
			$pluginstable = 'mambots';	
		}
				
		//create table for subitemtype moduleposition
			$database->setQuery("CREATE TABLE IF NOT EXISTS #__pi_subitem_youtube (
		  `id` int(11) NOT NULL auto_increment,
		  `item_id` int(11) NOT NULL,
		  `url` tinytext NOT NULL,
		  `size` tinytext NOT NULL,
		  PRIMARY KEY  (`id`)
		)");
			$database->query();
			
		//if first time plugin is used, enter default config in table		
		$database->setQuery( "SELECT params FROM #__".$pluginstable." WHERE element='itemtype_youtube' LIMIT 1 ");		
		$temp = $database->loadObjectList();		
		$plugin_config = '';
		if(count($temp) > 0){
			$temp = $temp[0];
			$plugin_config = $temp->params;
		}
		if($plugin_config==''){		
			$configuration = 'sizes=big;550x438,normal;425x344,small;290x243
default_size=425x344';
			//insert fresh config
			$database->setQuery( "UPDATE #__".$pluginstable." SET params='$configuration' WHERE element='itemtype_youtube' ");
			$database->query();		
		}//end if no config	
	
	}

	function item_save($item_id){
		global $database;
		
		//get database joomla 1.5
		if( defined('_JEXEC') ){			
			$database = JFactory::getDBO();
		}
	
		//get vars		
		$sub_item_id = intval($this->get_var('sub_item_id', ''));
		$url = $this->get_var('url', '', 'post');
		$size = $this->get_var('size', '', 'post');	
		
		if($sub_item_id==''){
			//if new item	
			
			$database->setQuery( "INSERT INTO #__pi_subitem_youtube SET item_id='$item_id', url='$url', size='$size'");
			if (!$database->query()) {
				echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>";
				exit();
			}
			
			//end if new item
		}else{
			//start update item
		
			$database->setQuery( "UPDATE #__pi_subitem_youtube SET url='$url', size='$size' WHERE id='$sub_item_id'"	);
			if (!$database->query()) {
				echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>";
				exit();
			}	
				
			//end update item
		}	
	}
	
	function item_delete($item_id){
		global $database;
		
		//get database joomla 1.5
		if( defined('_JEXEC') ){			
			$database = JFactory::getDBO();
		}
		
		//delete sub-item
		$database->setQuery("DELETE FROM #__pi_subitem_youtube WHERE item_id='$item_id'");
		if (!$database->query()) {
			echo "<script> alert('".$database -> getErrorMsg()."'); window.history.go(-1); </script>";
		}
		
	}
	
	function config_save(){			
		
		$sizes = $this->get_var('sizes',array(0));

		//if size is empty, take it out of array
		//added the 'm' because of the problem with numerical indexes when unsetting in loop
		$loops = count($sizes);
		for($n = 0; $n <= $loops; $n++){
			if($sizes['m'.$n][name]=='' || $sizes['m'.$n][width_height]==''){		
				unset ($sizes['m'.$n]);					
			} 	
		}	
		
		//write sizes array to config string
$item_specific_config_string = 'sizes=';
if(is_array($sizes)){
	$first = 1;	
	foreach($sizes as $size){
		if(!$first){
			$item_specific_config_string .= ',';
		}
		$item_specific_config_string .= $size[name].';'.$size[width_height];
		$first = 0;
	}	
}
$item_specific_config_string .= '
default_size='.$this->get_var('default_size', '').'
';
		return $item_specific_config_string;
		
		
		
		
	
	}











}

?>